//
//  FGRecorderViewController.m
//  VoiceRecoder
//
//  Created by Fengur on 16/9/1.
//  Copyright © 2016年 code.uworks.fengur. All rights reserved.
//

#import "FGRecordBottomView.h"
#import "FGRecordTopView.h"
#import "FGRecordWaveView.h"
#import "FGRecorderViewController.h"
#import "LVRecordTool.h"

@interface FGRecorderViewController ()<LVRecordToolDelegate, FGRecorderBottomViewDelegate> {
    LVRecordTool *_recordTool;
    BOOL _isRecording;
}

@end

@implementation FGRecorderViewController

- (void)viewDidLoad {
    _recordTool = [LVRecordTool sharedRecordTool];
    _isRecording = NO;
    _recordTool.delegate = self;
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem =
        [FGFastInit rightBarButtonItem:self
                                action:@selector(playRecordingFile)
                           rightString:@"播放"];
    self.title = @"录音";
    [self setupControls];
}

- (void)playRecordingFile {
    [_recordTool playRecordingFile];
}

- (void)recordTool:(LVRecordTool *)recordTool didstartRecoring:(double)currentValue {
    [_waveView updateWaveValueWithVoiceValue:currentValue];
}

- (void)makePoints {
    if (_recordTool.recorder.isRecording) {
        [_waveView makePoints];
    }
}


- (void)saveRecordingFile{
    
}

- (void)startOrEndRecording {
    _isRecording = !_isRecording;
    if (_isRecording) {
        [_recordTool startRecording];
        [_topView.countTimeLabel start];
    } else {
        [_recordTool stopRecording];
        [_topView.countTimeLabel pause];
    }
}

- (void)setupControls {
    _topView = [[FGRecordTopView alloc]
        initWithFrame:CGRectMake(0, 0, ScreenWidth, [FGSizeTool heightWithSize6:180])];
    [self.view addSubview:_topView];

    _waveView =
        [[FGRecordWaveView alloc] initWithFrame:CGRectMake(0, _topView.height, ScreenWidth,
                                                           [FGSizeTool heightWithSize6:240])];
    [self.view addSubview:_waveView];

    _bottomView = [[FGRecordBottomView alloc]
        initWithFrame:CGRectMake(0, _waveView.height + _waveView.y, ScreenWidth,
                                 [FGSizeTool heightWithSize6:183])];
    _bottomView.delegate = self;
    [self.view addSubview:_bottomView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
